package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.OutpatientRecharge;
import com.qf.service.OutpatientRechargeService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (OutpatientRecharge)表控制层
 *
 * @author makejava
 * @since 2024-04-30 10:54:20
 */
@RestController
@RequestMapping("/api/outpatientRecharge")
public class OutpatientRechargeController {
    /**
     * 服务对象
     */
    @Autowired
    private OutpatientRechargeService outpatientRechargeService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R selectOutpatientRechargeByCondition(@RequestParam Map<String,Object> map) {

        System.out.println("map = " + map);

        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<OutpatientRecharge> list = outpatientRechargeService.selectOutpatientRechargeByCondition(map);
        if (list != null && !list.isEmpty()) {
            PageInfo<OutpatientRecharge> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }

        return R.fail();
    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.outpatientRechargeService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param outpatientRecharge 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(OutpatientRecharge outpatientRecharge, HttpSession session) {

        // 设置session
        session.setAttribute("outpatientRecharge",outpatientRecharge);
        boolean result = outpatientRechargeService.insert(outpatientRecharge,session);
        if (result) {
            return R.ok();
        }
        return R.fail("增加失败");
    }
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    // id 是路径变量，它会被 @PathVariable 注解提取
    // 并作为方法的参数传递给 deleteById() 方法
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        boolean result = outpatientRechargeService.deleteById(id);
        if (result) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 编辑数据
     *
     * @param outpatientRecharge 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(OutpatientRecharge outpatientRecharge) {
        boolean result = outpatientRechargeService.update(outpatientRecharge);
        if (result) {
            return R.ok();
        }
        return R.fail("编辑失败");
    }


    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {
        boolean result = outpatientRechargeService.deleteBatch(list);

        if (result) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 查询所有
     */
    @RequestMapping("/findAllOutpatientRecharge")
    public List<OutpatientRecharge> findAllOutpatientRecharge() {
        return outpatientRechargeService.findAllOutpatientRecharge();
    }

    /**
     * 导出Excel表格
     * @param response
     */
    @RequestMapping("/Export")
    // HttpServletResponse直接响应，不需要@ResponseBody
    public void Export(HttpServletResponse response) {
        // 通过response响应数据，使用void
        outpatientRechargeService.exportExcel(response);
    }

    /**
     * 批量新增
     */
    @RequestMapping("/saveBath")
    public R insertBatch(@RequestParam List<OutpatientRecharge> list) {
        boolean result = outpatientRechargeService.insertBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }
    /**
     * 导入数据文件
     * @param file
     * @return
     */
    @RequestMapping("/importExcel")
    public R importExcel(@RequestParam MultipartFile file) {
        boolean result = outpatientRechargeService.importExcel(file);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }
}

